/*
       Licensed to the Apache Software Foundation (ASF) under one
       or more contributor license agreements.  See the NOTICE file
       distributed with this work for additional information
       regarding copyright ownership.  The ASF licenses this file
       to you under the Apache License, Version 2.0 (the
       "License"); you may not use this file except in compliance
       with the License.  You may obtain a copy of the License at

         http://www.apache.org/licenses/LICENSE-2.0

       Unless required by applicable law or agreed to in writing,
       software distributed under the License is distributed on an
       "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
       KIND, either express or implied.  See the License for the
       specific language governing permissions and limitations
       under the License.
 */
package org.apache.cordova;

import javafx.event.ActionEvent;
import javafx.event.Event;
import javafx.event.EventHandler;
import name.antonsmirnov.javafx.dialog.Dialog;
import name.antonsmirnov.javafx.dialog.Dialog.Builder;

import org.apache.cordova.api.CallbackContext;
import org.apache.cordova.api.CordovaPlugin;
import org.apache.cordova.api.PluginResult;
import org.json.JSONArray;
import org.json.JSONException;

/**
 * This class provides access to notifications on the device.
 * FIXME CJO - not finish at all (simple alert only done)
 * @author Cyril Joui
 */
public class Notification extends CordovaPlugin {

	public int confirmResult = -1;

	/**
	 * Constructor.
	 */
	public Notification() {
	}

	/**
	 * Executes the request and returns PluginResult.
	 * 
	 * @param action
	 *            The action to execute.
	 * @param args
	 *            JSONArray of arguments for the plugin.
	 * @param callbackContext
	 *            The callback context used when calling back into JavaScript.
	 * @return True when the action was valid, false otherwise.
	 */
	public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
		if (action.equals("beep")) {
			// this.beep(args.getLong(0));
		} else if (action.equals("vibrate")) {
			// this.vibrate(args.getLong(0));
		} else if (action.equals("alert")) {
			this.alert(args.getString(0), args.getString(1), args.getString(2), callbackContext);
			return true;
		} else if (action.equals("confirm")) {
			 this.confirm(args.getString(0), args.getString(1),
					 args.getString(2), callbackContext);
			return true;
		} else if (action.equals("activityStart")) {
			// this.activityStart(args.getString(0), args.getString(1));
		} else if (action.equals("activityStop")) {
			// this.activityStop();
		} else if (action.equals("progressStart")) {
			// this.progressStart(args.getString(0), args.getString(1));
		} else if (action.equals("progressValue")) {
			// this.progressValue(args.getInt(0));
		} else if (action.equals("progressStop")) {
			// this.progressStop();
		} else {
			return false;
		}

		// Only alert and confirm are async.
		callbackContext.success();
		return true;
	}

	/**
	 * Builds and shows a native Android alert with given Strings
	 * 
	 * @param message
	 *            The message the alert should display
	 * @param title
	 *            The title of the alert
	 * @param buttonLabel
	 *            The label of the button
	 * @param callbackContext
	 *            The callback context
	 */
	public synchronized void alert(final String message, final String title, final String buttonLabel, final CallbackContext callbackContext) {
		final Dialog showInfoDialog = Dialog.showInfo(title, message, buttonLabel);
		showInfoDialog.getOkButton().setOnAction(new EventHandler<ActionEvent>() {

			@Override
			public void handle(ActionEvent e) {
				showInfoDialog.close();
				callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, 0));
			}
		});
	}

    /**
     * Builds and shows a native Android confirm dialog with given title, message, buttons.
     * This dialog only shows up to 3 buttons.  Any labels after that will be ignored.
     * The index of the button pressed will be returned to the JavaScript callback identified by callbackId.
     *
     * @param message           The message the dialog should display
     * @param title             The title of the dialog
     * @param buttonLabels      A comma separated list of button labels (Up to 3 buttons)
     * @param callbackContext   The callback context.
     */
    public synchronized void confirm(final String message, final String title, String buttonLabels, final CallbackContext callbackContext) {

        final String[] fButtons = buttonLabels.split(",");

        Builder builder = Dialog.buildConfirmation(title, message);

        // First button
        if (fButtons.length > 0) {
        	builder.addButton(fButtons[0], new EventHandler<Event>() {
				@Override
				public void handle(Event e) {
					callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, 1));
				}
			});
        }

        // Second button
        if (fButtons.length > 1) {
        	builder.addButton(fButtons[1], new EventHandler<Event>() {
				@Override
				public void handle(Event e) {
					callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, 2));
				}
			});

        }

        // Third button
        if (fButtons.length > 2) {
        	builder.addButton(fButtons[2], new EventHandler<Event>() {
				@Override
				public void handle(Event e) {
					callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, 3));
				}
			});

        }
        /*
        dlg.setOnCancelListener(new AlertDialog.OnCancelListener() {
            public void onCancel(DialogInterface dialog)
            {
                dialog.dismiss();
                callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, 0));
            }
        });
        */

        builder.build().show();
    }
}
